<?php
namespace app\common\controller;
use app\common\controller\Push;
use app\common\ceshi_model\Alarm;
use app\common\ceshi_model\Alarmb;
use app\common\ceshi_model\Alarmc;
use app\common\ceshi_model\Device;
use think\Cache;
use app\common\ceshi_model\User;


/**
 * Class Realdata
 * @package app\index\controller
 */
class CPushmsg  extends \think\Controller
{
    public function __construct() {
        header("Access-Control-Allow-Origin:*");
        header("Access-Control-Allow-Method:POST,GET");
    }

    function test(){
        $push = new Push();
        $push->pushMessageToSingle('吕重阳 测试','94effee2edff4167ca044301aa2319d1');
    }


    /********************************处理推送消息************************************/
    protected  $colnamearr = array('ua_down','ub_down','uc_down','ua_over','ub_over','uc_over','ua_under',
                                      'ub_under','uc_under','ia_over','ib_over','ic_over','volt_imbalance','curr_imbalance',
                                      'cosa_down','cosb_down','cosc_down','cosq_down');
    protected $zwnamearr =  array('A相断电','B相断电','C相断电','A相过压','B相过压','C相过压','A相欠压','B相欠压',
                                     'C相欠压','A相过流','B相过流','C相过流','电压不平衡度','电流不平衡度','A相功率因数',
                                     'B相功率因数','C相功率因数','合相功率因数');
    public function  push_msg(){
        $ala = new Alarm();
//        $alb = new Alarmb();
//        $alc = new Alarmc();

        $con = '';
        $timekey = config('project_name').'alarm_timea';
        $altimea = Cache::get($timekey);
//        $altimeb = Cache::get('alarm_timeb');
//        $altimec = Cache::get('alarm_timec');


        if(empty($altimea)){
            $cona = "CreateTime>DATEADD(mi,-5,GETDATE())";
        }else{
            $cona = "CreateTime>'$altimea'";
        }

//        if(empty($altimeb)){
//            $conb = "CreateTime>DATEADD(mi,-5,GETDATE())";
//        }else{
//            $conb = "CreateTime>'$altimeb'";
//        }
//
//        if(empty($altimec)){
//            $conc = "CreateTime>DATEADD(mi,-5,GETDATE())";
//        }else{
//            $conc = "CreateTime>'$altimec'";
//        }
        // 查询报警信息
        $resa = $ala->where($cona)
                    ->order('CreateTime desc')
                   ->select();


//        $resb = $alb->where($conb)->order('CreateTime desc')->select();
//        $resc = $alc->where($conc)->order('CreateTime desc')->select();

        if($resa && $resa[0]){
            $altimea = Cache::set($timekey,$resa[0]['CreateTime']);
//            $altimeb = Cache::set('alarm_timeb',$resa[0]['CreateTime']);
//            $altimec = Cache::set('alarm_timec',$resa[0]['CreateTime']);
        }

        $this->hand_record($resa);
//        $this->hand_record($resb);
//        $this->hand_record($resc);
    }



    /**
     * 处理记录
     * @param $record
     */
    protected  function hand_record($record){
        $len = count($this->colnamearr);
        foreach($record as $res){
            $str = '';
            $wshop = $res['WrokShop'];
            for ($i = 0 ; $i< $len; $i++){
                $val = $this->colnamearr[$i] ;
                if ( !empty($val) && $res[$val] != 0 ){
                    $str .=  ','. $this->zwnamearr[$i] ;
                }
            }

            if (!empty($str)){
                $str = $res['DeviceName'].$str.'报警';
                $this->send_msg_touser($wshop,$str);
            }
        }
    }

    /**
     * 发送消息
     * @param $did
     * @param $str
     */
    protected function  send_msg_touser($wshop,$str){
        if(empty($str)){
            return;
        }
        if(strpos($str,'断电')!==false){
            return;
        }

        $push = new Push();
        //根据工作空间，找人
        $devm = new Device();
        $umodel = new User();
        $res = $devm->field('distinct Factory')->where(array('WorkShop'=>$wshop))->find();
        $ulst = $umodel->field('Pushid')->where(array('Factory'=>$res['Factory']))->select();

        //发送消息
        foreach ($ulst as $u){
            if ($u['Pushid']){
                $push->pushMessageToSingle($str,$u['Pushid']);
            }
        }
    }


}
